package com.feedgraph;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Context;
import android.content.CursorLoader;
import android.database.Cursor;
import android.location.Location;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;

public class PlacesManager {

	public static final String ID = "_ID";
	public static final String NAME = "Name";
	public static final String ICON = "Icon";
	public static final String LONGITUDE = "Longitude";
	public static final String LATITUDE = "Latitude";
	public static final String RADIUS = "Radius";
	public static final String TIME_STAMP = "TimeStamp";
  public static final Uri CONTENT_URI = Uri.parse("content://"  + PlacesContentProvider.AUTHORITY + "/Places");
	public static final String PLACES_CONTENT_TYPE = "vnd.android.cursor.dir/com.bottlcap.app.places";
	private static final String [] PROJECTION_ALL = new String[] {ID, NAME, ICON, LONGITUDE, LATITUDE, RADIUS, TIME_STAMP};
	private Context mContext;
	
	public PlacesManager(Context context ) {
		mContext = context;
	}
	
	public CursorLoader makeLoader() {
		return new CursorLoader(mContext, CONTENT_URI, PROJECTION_ALL, null, null, TIME_STAMP + " DESC");
	}
	
	static public class Place implements Comparable <Place>, Parcelable {
		public static final String EXTRA = "com.feedgraph.PlacesManager.Places.EXTRA";
		public String mName;
		public String mId;
		public String mIcon;
		public double mLongitude, mLatitude;
		public float mRadius;
		public String mTimeStamp;

		public Place (JSONObject json) {
			mName = json.optString ("Name");
			mId = json.optString ("ID");
			mIcon = json.optString ("Icon");
			mLatitude = json.optDouble ("Latitude");
			mLongitude = json.optDouble ("Longitude");
			mRadius = (float) json.optDouble ("Radius", 100);
			mTimeStamp = json.optString ("TimeStamp");
		}

		public Place (Parcel in) {
			mName = in.readString ();
			mId = in.readString ();
			mIcon = in.readString ();
			mLatitude = in.readDouble ();
			mLongitude = in.readDouble ();
			mRadius = in.readFloat ();
			mTimeStamp = in.readString ();
		}

		public Place (String name) {
			this.mName = name;
		}

		public Place (Cursor c) {
			//_id TEXT PRIMARY KEY, Name TEXT, Icon TEXT, Longitude REAL, Latitude REAL, Radius REAL, TimeStamp TEXT
			mId = c.getString (0);
			mName = c.getString (1);
			mIcon = c.getString (2);
			mLongitude = c.getDouble (3);
			mLatitude = c.getDouble(4);
			mRadius = c.getFloat(5);
			mTimeStamp = c.getString(6);
		}

		@Override
		public int compareTo (Place another) {
			return mId.compareTo (another.mId);
		}
		
		@Override
		public boolean equals (Object another ) {
			return mId.equals (((Place) another).mId);
		}

		public Location getLocation (String provider) {
			Location loc = new Location (provider);
			loc.setLatitude (mLatitude);
			loc.setLongitude (mLongitude);

			return loc;
		}

		public Uri getMapURI () {
			return Uri.parse ("geo:" + mLatitude + "," + mLongitude + "?z=15&q=" + Uri.encode (mName));
		}

		public JSONObject toJSON () throws JSONException {

			JSONObject json = new JSONObject ();
			json.put ("Name", mName);
			json.put ("ID", mId);
			json.put ("Icon", mIcon);
			json.put ("Latitude", mLatitude);
			json.put ("Longitude", mLongitude);
			json.put ("Radius", mRadius);
			json.put ("TimeStamp", mTimeStamp);
			
			return json;
		}

		@Override
		public int describeContents () {
			return 0;
		}

		@Override
		public void writeToParcel (Parcel dest, int flags) {
			dest.writeString (mName);
			dest.writeString (mId);
			dest.writeString (mIcon);
			dest.writeDouble (mLongitude);
			dest.writeDouble (mLatitude);
			dest.writeFloat (mRadius);
			dest.writeString(mTimeStamp);

		}

		public static final Parcelable.Creator <Place> CREATOR = new Parcelable.Creator <Place> () {
			public Place createFromParcel (Parcel in) {
				return new Place (in);
			}

			public Place [] newArray (int size) {
				return new Place [size];
			}
		};

		public ContentValues toContentValues () {
			ContentValues values = new ContentValues();
			values.put (PlacesManager.ID, mId);
			values.put (PlacesManager.NAME, mName);
			values.put (PlacesManager.ICON, mIcon);
			values.put(PlacesManager.LATITUDE, mLatitude);
			values.put (PlacesManager.LONGITUDE, mLongitude);
			values.put (PlacesManager.RADIUS, mRadius);
			values.put (PlacesManager.TIME_STAMP, mTimeStamp);
			
			return values;
		}

	}

}
